package com.example.mywanandroid.project.ui;

import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mywanandroid.home.WebViewActivity;
import com.example.mywanandroid.project.business.ParseData;
import com.example.mywanandroid.project.business.GetData;
import com.example.mywanandroid.project.ProjectRecyclerAdapter;
import com.example.mywanandroid.project.ProjectRecyclerViewItem;
import com.example.mywanandroid.project.business.GetProjectRecyclerViewCallBack;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

public class InitComponents {
    private static int currentPage = 2;
    private ProjectRecyclerAdapter projectRecyclerAdapter;
    // 初始化RecyclerView界面
    public void initRecyclerView(RecyclerView recyclerView, final List<ProjectRecyclerViewItem> recList, final FragmentActivity currentActivity) {
        projectRecyclerAdapter = new ProjectRecyclerAdapter(currentActivity, currentActivity, recList);
        projectRecyclerAdapter.setOnItemClickListener(new ProjectRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                String link = recList.get(position).getLink();
                Intent intent = new Intent(currentActivity, WebViewActivity.class);
                intent.putExtra("link", link);
                currentActivity.startActivity(intent);
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(currentActivity, LinearLayoutManager.VERTICAL, false));
        recyclerView.addItemDecoration(new DividerItemDecoration(currentActivity, DividerItemDecoration.VERTICAL));
        recyclerView.setAdapter(projectRecyclerAdapter);
    }

    // 初始化下拉刷新功能
    public void initRefresh(RefreshLayout refreshLayout, final FragmentActivity currentActivity) {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                GetData.getProjectRecyclerViewData(1, new GetProjectRecyclerViewCallBack() {
                    @Override
                    public void onGetProjectRecyclerViewCallBack(String rawData) {
                        final List<ProjectRecyclerViewItem> extraList = ParseData.parseProjectRecyclerViewData(rawData);
                        currentActivity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                projectRecyclerAdapter.refresh(extraList);
                            }
                        });
                    }
                });
                refreshLayout.finishRefresh(200/*, false*/);// 传入false表示刷新失败
            }
        });
        //设置 Header 为 贝塞尔雷达样式
        refreshLayout.setRefreshHeader(new BezierRadarHeader(currentActivity).setEnableHorizontalDrag(true));
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                GetData.getProjectRecyclerViewData(currentPage++, new GetProjectRecyclerViewCallBack() {
                    @Override
                    public void onGetProjectRecyclerViewCallBack(String rawData) {
                        final List<ProjectRecyclerViewItem> extraList = ParseData.parseProjectRecyclerViewData(rawData);
                        currentActivity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                projectRecyclerAdapter.add(extraList);
                            }
                        });
                    }
                });
                refreshLayout.finishLoadMore(true);//加载完成
            }
        });
    }
}
